package com.anos.smsfun.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.anos.smsfun.R;
import com.anos.smsfun.model.MessageItem;
import com.anos.smsfun.utility.DefConstants;

public class ConversationListAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<MessageItem> mData;
	private static LayoutInflater inflater = null;
	private MessageItem messageItem = null;

	public ConversationListAdapter(Context c, ArrayList<MessageItem> data) {
		mContext = c;
		mData = data;
		inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	/** Create a holder Class to contain inflated xml file elements */
	public static class ViewHolder {
		public ImageView imv;
		public TextView tvName;
		public TextView tvBody;
		public TextView tvTime;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		ViewHolder holder = null;
		if (vi == null) {
			vi = inflater.inflate(R.layout.item_conversation_list, null);

			holder = new ViewHolder();
			holder.imv = (ImageView) vi
					.findViewById(R.id.imvPic_item_conv_list);
			holder.tvName = (TextView) vi
					.findViewById(R.id.tvName_item_conv_list);
			holder.tvBody = (TextView) vi
					.findViewById(R.id.tvBody_item_conv_list);
			holder.tvTime = (TextView) vi
					.findViewById(R.id.tvTime_item_conv_list);

			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}

		if (this.mData.size() > position) {
			messageItem = this.mData.get(position);

			if (messageItem.isSelected()) {
				vi.setBackgroundColor(Color.GRAY);
			} else {
				vi.setBackgroundDrawable(mContext.getResources().getDrawable(
						R.drawable.bg_row_item_listview_selector));
			}

			if (!messageItem.getNameDisplay().equals(""))
				holder.tvName.setText(messageItem.getNameDisplay());
			else
				holder.tvName.setText(messageItem.getAddress());

			String temp = messageItem.getBody();
			if (messageItem.getBody().length() > 35) {
				temp = messageItem.getBody().substring(0, 35) + "...";
			}
			holder.tvBody.setText(temp);
			holder.tvTime.setText(messageItem
					.getFormatDate(DefConstants.DATE_FORMAT_DDMMYYYY));

			// danh dau tin nhan chua doc
			if (messageItem.getSeen() == 0) {
				holder.tvName.setTypeface(Typeface.DEFAULT_BOLD);
				holder.tvBody.setTypeface(Typeface.DEFAULT_BOLD);
			} else {
				holder.tvName.setTypeface(Typeface.DEFAULT);
				holder.tvBody.setTypeface(Typeface.DEFAULT);
			}
		}

		return vi;
	}
}
